package com.ccl.hdnumberkeyboard;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.Keyboard.Key;
import android.inputmethodservice.KeyboardView;
import android.util.AttributeSet;

import java.util.List;

public class HDKeyBoardView extends KeyboardView {
    // 用于区分左下角空白的按键
    private static final int KEYCODE_EMPTY = -10;
    private Context mContext;
    private Keyboard mKeyBoard;

    private int mDeleteWidth;
    private int mDeleteHeight;
    private int mDeleteBackgroundColor;
    private Drawable mDeleteDrawable;
    private Rect mDeleteDrawRect;

    private int mEmptyWidth;
    private int mEmptyHeight;
    private int mEmptyBackgroundColor;
    private Drawable mEmptyDrawable;
    private Rect mEmptyDrawRect;

    private boolean isDrawEmpty;

    public HDKeyBoardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init(context, attrs,0);
    }

    public HDKeyBoardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
        init(context, attrs,defStyle);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, com.ccl.hdnumberkeyboard.R.styleable.XNumberKeyboardView,
                defStyleAttr, 0);
        mDeleteDrawable = typedArray.getDrawable(com.ccl.hdnumberkeyboard.R.styleable.XNumberKeyboardView_xnkv_deleteDrawable);
        mDeleteBackgroundColor = typedArray.getColor(
                com.ccl.hdnumberkeyboard.R.styleable.XNumberKeyboardView_xnkv_deleteBackgroundColor, Color.TRANSPARENT);
        mDeleteWidth = typedArray.getDimensionPixelOffset(com.ccl.hdnumberkeyboard.R.styleable.XNumberKeyboardView_xnkv_deleteWidth,
                -1);
        mDeleteHeight = typedArray.getDimensionPixelOffset(com.ccl.hdnumberkeyboard.R.styleable.XNumberKeyboardView_xnkv_deleteHeight,
                -1);

        mEmptyDrawable = typedArray.getDrawable(com.ccl.hdnumberkeyboard.R.styleable.XNumberKeyboardView_xnkv_emptyDrawable);
        mEmptyBackgroundColor = typedArray.getColor(
                com.ccl.hdnumberkeyboard.R.styleable.XNumberKeyboardView_xnkv_emptyBackgroundColor, Color.TRANSPARENT);
        mEmptyWidth = typedArray.getDimensionPixelOffset(com.ccl.hdnumberkeyboard.R.styleable.XNumberKeyboardView_xnkv_emptyWidth,
                -1);
        mEmptyHeight = typedArray.getDimensionPixelOffset(com.ccl.hdnumberkeyboard.R.styleable.XNumberKeyboardView_xnkv_emptyHeight,
                -1);

        typedArray.recycle();

    }

    /**
     * 重新画一些按键
     */
    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mKeyBoard = this.getKeyboard();
        List<Key> keys = null;
        if (mKeyBoard != null) {
            keys = mKeyBoard.getKeys();
        }

        if (keys != null) {
            for (Key key : keys) {

                // 如果是左下角空白的按键，重画按键的背景
                if (key.codes[0] == KEYCODE_EMPTY && isDrawEmpty) {
//                    drawKeyBackground(R.drawable.icon_key_contact, canvas, key);
                    drawEmptyButton(key,canvas);
                }
                // 如果是右下角的删除按键，重画按键的背景，并且绘制删除图标
                else if (key.codes[0] == Keyboard.KEYCODE_DELETE) {
//                    drawKeyBackground(R.drawable.icon_key_delete, canvas, key);
                    drawDeleteButton(key,canvas);
                }
            }
        }
    }

    private void drawKeyBackground(int drawableId, Canvas canvas, Key key) {//
        Drawable npd = mContext.getResources().getDrawable(
                drawableId);
        int[] drawableState = key.getCurrentDrawableState();
        if (key.codes[0] != 0) {
            npd.setState(drawableState);
        }
        npd.setBounds(key.x, key.y, key.x + key.width, key.y
                + key.height);
        npd.draw(canvas);

    }

    // 绘制删除按键
    private void drawDeleteButton(Key key, Canvas canvas) {
        if (mDeleteDrawable == null) {
            return;
        }

        // 计算删除图标绘制的坐标
        if (mDeleteDrawRect == null || mDeleteDrawRect.isEmpty()) {
            int drawWidth, drawHeight;
            int intrinsicWidth = mDeleteDrawable.getIntrinsicWidth();
            int intrinsicHeight = mDeleteDrawable.getIntrinsicHeight();

            if (mDeleteWidth > 0 && mDeleteHeight > 0) {
                drawWidth = mDeleteWidth;
                drawHeight = mDeleteHeight;
            } else if (mDeleteWidth > 0 && mDeleteHeight <= 0) {
                drawWidth = mDeleteWidth;
                drawHeight = drawWidth * intrinsicHeight / intrinsicWidth;
            } else if (mDeleteWidth <= 0 && mDeleteHeight > 0) {
                drawHeight = mDeleteHeight;
                drawWidth = drawHeight * intrinsicWidth / intrinsicHeight;
            } else {
                drawWidth = intrinsicWidth;
                drawHeight = intrinsicHeight;
            }

            // 限制图标的大小，防止图标超出按键
            if (drawWidth > key.width) {
                drawWidth = key.width;
                drawHeight = drawWidth * intrinsicHeight / intrinsicWidth;
            }
            if (drawHeight > key.height) {
                drawHeight = key.height;
                drawWidth = drawHeight * intrinsicWidth / intrinsicHeight;
            }

            // 获取删除图标绘制的坐标
            int left = key.x + (key.width - drawWidth) / 2;
            int top = key.y + (key.height - drawHeight) / 2;
            mDeleteDrawRect = new Rect(left, top, left + drawWidth, top + drawHeight);
        }

        // 绘制删除的图标
        if (mDeleteDrawRect != null && !mDeleteDrawRect.isEmpty()) {
            mDeleteDrawable.setBounds(mDeleteDrawRect.left, mDeleteDrawRect.top,
                    mDeleteDrawRect.right, mDeleteDrawRect.bottom);
            mDeleteDrawable.draw(canvas);
        }
    }

    private void drawEmptyButton(Key key, Canvas canvas) {
        if (mEmptyDrawable == null) {
            return;
        }

        // 计算删除图标绘制的坐标
        if (mEmptyDrawRect == null || mEmptyDrawRect.isEmpty()) {
            int drawWidth, drawHeight;
            int intrinsicWidth = mEmptyDrawable.getIntrinsicWidth();
            int intrinsicHeight = mEmptyDrawable.getIntrinsicHeight();

            if (mEmptyWidth > 0 && mEmptyHeight > 0) {
                drawWidth = mEmptyWidth;
                drawHeight = mEmptyHeight;
            } else if (mEmptyWidth > 0 && mEmptyHeight <= 0) {
                drawWidth = mEmptyWidth;
                drawHeight = drawWidth * intrinsicHeight / intrinsicWidth;
            } else if (mEmptyWidth <= 0 && mEmptyHeight > 0) {
                drawHeight = mEmptyHeight;
                drawWidth = drawHeight * intrinsicWidth / intrinsicHeight;
            } else {
                drawWidth = intrinsicWidth;
                drawHeight = intrinsicHeight;
            }

            // 限制图标的大小，防止图标超出按键
            if (drawWidth > key.width) {
                drawWidth = key.width;
                drawHeight = drawWidth * intrinsicHeight / intrinsicWidth;
            }
            if (drawHeight > key.height) {
                drawHeight = key.height;
                drawWidth = drawHeight * intrinsicWidth / intrinsicHeight;
            }

            // 获取删除图标绘制的坐标
            int left = key.x + (key.width - drawWidth) / 2;
            int top = key.y + (key.height - drawHeight) / 2;
            mEmptyDrawRect = new Rect(left, top, left + drawWidth, top + drawHeight);
        }

        // 绘制删除的图标
        if (mEmptyDrawRect != null && !mEmptyDrawRect.isEmpty()) {
            mEmptyDrawable.setBounds(mEmptyDrawRect.left, mEmptyDrawRect.top,
                    mEmptyDrawRect.right, mEmptyDrawRect.bottom);
            mEmptyDrawable.draw(canvas);
        }
    }

    public void setEmptyButtonFlag(boolean isDrawEmpty){
       this.isDrawEmpty = isDrawEmpty;
        postInvalidate();
    }


}
